package self.realm;

import java.sql.*;

/**
 * Created by hasee on 2018/1/29.
 */
public class DbUtil {

   private DbUtil(){

   }

    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");//jdbc

        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        }//加载驱动程序);

    }

    public static Connection getConn(){
        Connection conn =null;
        try {

            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_shiro","root","root");//url+用户+密码
        } catch (SQLException e) {

            e.printStackTrace();
        }
        return conn;


    }

    public static PreparedStatement getPstmt(Connection conn,String sql){
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
        } catch (SQLException e) {


            e.printStackTrace();
        }
        return pstmt;
    }

    public static ResultSet getRs(PreparedStatement pstmt){
        ResultSet rs = null;
        try {
            rs = pstmt.executeQuery();
        } catch (SQLException e) {

            e.printStackTrace();
        }
        return rs;
    }
    public static void close(ResultSet rs,Statement stmt,Connection conn){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {

                e.printStackTrace();
            }
            rs=null;
        }


        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {

                e.printStackTrace();
            }
            stmt=null;
        }



        if(conn!= null){
            try {
                conn.close();
            } catch (SQLException e) {

                e.printStackTrace();
            }
            conn=null;
        }

    }

    public static void main(String[] args) {

        Connection conn = getConn();
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
